﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CardLogistics.DAL;
using System.Text;
using CardLogistics.Web.Components;

namespace CardLogistics.Web.MainPage.Report.DepReport
{
    public partial class J_MonthInv : BasePage
    {
        private static readonly DepReportData DepDAL = new DepReportData();
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            //if (txt_Data.Text.Trim().Length > 0)
            //{
            //    string startV = txt_Data.Text + "-01";//统计月份的第一天
            //    string endV = DateTime.Parse(startV).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");//统计月份的最后一天
            //    BindData(startV, endV);
            //}
            //else
            //{
            //    ShowMessageBox("请选择统计月份！");
            //    txt_Data.Focus();
            //}
            //20170418 彭巧 （修改月报表按时间段查询）
            if (txt_StartDate.Text.Trim().Length > 0 && txt_EndDate.Text.Trim().Length > 0)
            {
                //string startV = txt_Data.Text + "-01";//统计月份的第一天
                //string endV = DateTime.Parse(startV).AddMonths(1).AddDays(-1).ToString("yyyy-MM-dd");//统计月份的最后一天
                string startV = txt_StartDate.Text.Trim();//统计开始时间
                string endV = txt_EndDate.Text.Trim();//统计结束时间
                BindData(startV, endV);
            }
            else
            {
                ShowMessageBox("请选择统计月份！");
                txt_Data.Focus();
            }
        }
        private void BindData(string startData, string endData)
        {
            DataTable dt = DepDAL.GetJMonthInv(startData, endData);

            StringBuilder sbStr = new StringBuilder();
            sbStr.Append("<div style='text-align: center; font-size: 25px; font-weight: bold'>西安城市一卡通有限责任公司卡片统计月报表</div>");
            sbStr.Append("<table  style='border:0;width:100%;'><tr><td colspan='5' style='border:0;text-align:left;'>填报单位:</td><td  colspan='" + (dt.Rows.Count * 3 - 4) + "' style='border:0;text-align:right;'>填表日期:" + DateTime.Now.ToString("yyyy-MM-dd") + "</td></tr></table>");
            sbStr.Append("<table  border='1' style='font-size:16px;white-space: nowrap; '>");
            StringBuilder sbThCard = new StringBuilder("<tr><td rowspan='2'colspan='2'></td>");//卡类型名称
            StringBuilder sbThNPT = new StringBuilder("<tr>");//数量、单据、总计
            StringBuilder sbThSQ_BaiKa = new StringBuilder("<tr><td rowspan='4' style='width:90px;'>上期结存</td><td style='width:80px;'>白卡</td>");//上期结存-白卡
            StringBuilder sbThSQ_WSK = new StringBuilder("<tr><td>未售卡</td>");//上期结存-未售卡
            StringBuilder sbThSQ_ZKHK = new StringBuilder("<tr><td>制卡坏卡</td>");//上期结存-未售卡
            StringBuilder sbThSQ_WSFK = new StringBuilder("<tr><td>未售废卡</td>");//上期结存-未售卡

            StringBuilder sbThBQ_BaiKa = new StringBuilder("<td colspan='2'>本期入库</td>");//半期结存-白卡
            StringBuilder sbThBQ_ZKHK = new StringBuilder("<td colspan='2'>本期初制卡坏卡</td>");//半期结存-制卡坏卡
            StringBuilder sbTHBQ_FC = new StringBuilder("<td colspan='2'>本期发出</td>");//半期结存-本期发出
            StringBuilder sbTHBQ_WSHK = new StringBuilder("<td colspan='2'>本期收回未售坏卡</td>");//半期结存-本期收回未售坏卡
            StringBuilder sbTHBQ_FHCJ = new StringBuilder("<td colspan='2'>本期返回厂家坏卡</td>");//半期结存-本期返回厂家坏卡

            StringBuilder sbThKC_BaiKa = new StringBuilder("<tr><td rowspan='4'>库存</td><td>白卡</td>");//结存-白卡
            StringBuilder sbThKC_WSK = new StringBuilder("<tr><td>未售卡</td>");//结存-未售卡
            StringBuilder sbThKC_ZKHK = new StringBuilder("<tr><td>制卡坏卡</td>");//结存-未售卡
            StringBuilder sbThKC_WSFK = new StringBuilder("<tr><td>未售废卡</td>");//结存-未售卡未售废卡

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbThCard.Append("<td colspan='3'>" + dt.Rows[i]["cardtypename"].ToString() + "</td>");
                sbThNPT.Append("<td style='width:90px;'>数 量(张)</td><td style='width:90px;'>单 价(元)</td><td style='width:90px;'>金 额(元)</td>");
                sbThSQ_BaiKa.Append("<td>" + dt.Rows[i]["上期白卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期白卡金额"].ToString() + "</td>");
                sbThSQ_WSK.Append("<td>" + dt.Rows[i]["上期未售卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期未售卡金额"].ToString() + "</td>");
                sbThSQ_ZKHK.Append("<td>" + dt.Rows[i]["上期制卡坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期制卡坏卡金额"].ToString() + "</td>");
                sbThSQ_WSFK.Append("<td>" + dt.Rows[i]["上期未售废卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["上期未售废卡金额"].ToString() + "</td>");

                sbThBQ_BaiKa.Append("<td>" + dt.Rows[i]["本期入库"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期入库金额"].ToString() + "</td>");
                sbThBQ_ZKHK.Append("<td>" + dt.Rows[i]["本期初制卡坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期初制卡坏卡金额"].ToString() + "</td>");
                sbTHBQ_FC.Append("<td>" + dt.Rows[i]["本期发出"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期发出金额"].ToString() + "</td>");
                sbTHBQ_WSHK.Append("<td>" + dt.Rows[i]["本期收回未售坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期收回未售坏卡金额"].ToString() + "</td>");
                sbTHBQ_FHCJ.Append("<td>" + dt.Rows[i]["本期返回厂家坏卡"].ToString() + "</td><td></td><td>" + dt.Rows[i]["本期返回厂家坏卡金额"].ToString() + "</td>");

                sbThKC_BaiKa.Append("<td>" + dt.Rows[i]["白卡库存"].ToString() + "</td><td></td><td>" + dt.Rows[i]["白卡库存金额"].ToString() + "</td>");
                sbThKC_WSK.Append("<td>" + dt.Rows[i]["未售卡库存"].ToString
                    () + "</td><td></td><td>" + dt.Rows[i]["未售卡库存金额"].ToString() + "</td>");
                sbThKC_ZKHK.Append("<td>" + dt.Rows[i]["制卡坏卡库存"].ToString() + "</td><td></td><td>" + dt.Rows[i]["制卡坏卡库存金额"].ToString() + "</td>");
                sbThKC_WSFK.Append("<td>" + dt.Rows[i]["未售坏卡库存"].ToString() + "</td><td></td><td>" + dt.Rows[i]["未售坏卡库存金额"].ToString() + "</td>");

            }

            sbThCard.Append("<td colspan='2'>合计</td></tr>");
            sbThNPT.Append("<td style='width:90px;'>数 量(张)</td><td style='width:90px;'>金 额(元)</td></tr>");
            sbThSQ_BaiKa.Append("<td>" + dt.Compute("sum(上期白卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期白卡金额)", "true").ToString() + "</td></tr>");
            sbThSQ_WSK.Append("<td>" + dt.Compute("sum(上期未售卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期未售卡金额)", "true").ToString() + "</td></tr>");
            sbThSQ_ZKHK.Append("<td>" + dt.Compute("sum(上期制卡坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期制卡坏卡金额)", "true").ToString() + "</td></tr>");
            sbThSQ_WSFK.Append("<td>" + dt.Compute("sum(上期未售废卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(上期未售废卡)", "true").ToString() + "</td></tr>");

            sbThBQ_BaiKa.Append("<td>" + dt.Compute("sum(本期入库)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期入库金额)", "true").ToString() + "</td></tr>");
            sbThBQ_ZKHK.Append("<td>" + dt.Compute("sum(本期初制卡坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期初制卡坏卡金额)", "true").ToString() + "</td></tr>");
            sbTHBQ_FC.Append("<td>" + dt.Compute("sum(本期发出)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期发出金额)", "true").ToString() + "</td></tr>");
            sbTHBQ_WSHK.Append("<td>" + dt.Compute("sum(本期收回未售坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期收回未售坏卡金额)", "true").ToString() + "</td></tr>");
            sbTHBQ_FHCJ.Append("<td>" + dt.Compute("sum(本期返回厂家坏卡)", "true").ToString() + "</td><td>" + dt.Compute("sum(本期返回厂家坏卡金额)", "true").ToString() + "</td></tr>");

            sbThKC_BaiKa.Append("<td>" + dt.Compute("sum(白卡库存)", "true").ToString() + "</td><td>" + dt.Compute("sum(白卡库存金额)", "true").ToString() + "</td></tr>");
            sbThKC_WSK.Append("<td>" + dt.Compute("sum(未售卡库存)", "true").ToString() + "</td><td>" + dt.Compute("sum(未售卡库存金额)", "true").ToString() + "</td></tr>");
            sbThKC_ZKHK.Append("<td>" + dt.Compute("sum(制卡坏卡库存)", "true").ToString() + "</td><td>" + dt.Compute("sum(制卡坏卡库存金额)", "true").ToString() + "</td></tr>");
            sbThKC_WSFK.Append("<td>" + dt.Compute("sum(未售坏卡库存)", "true").ToString() + "</td><td>" + dt.Compute("sum(未售坏卡库存金额)", "true").ToString() + "</td></tr>");


            sbStr.Append(sbThCard).Append(sbThNPT).Append(sbThSQ_BaiKa).Append(sbThSQ_WSK).Append(sbThSQ_ZKHK).Append(sbThSQ_WSFK);
            sbStr.Append(sbThBQ_BaiKa).Append(sbThBQ_ZKHK).Append(sbTHBQ_FC).Append(sbTHBQ_WSHK).Append(sbTHBQ_FHCJ);
            sbStr.Append(sbThKC_BaiKa).Append(sbThKC_WSK).Append(sbThKC_ZKHK).Append(sbThKC_WSFK);

            d_info.InnerHtml = sbStr.ToString() + "</table>";
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (d_info.InnerHtml.Length > 50)
            {
                ExportExcel(txt_Data.Text, d_info);
            }
            else
            {
                ShowMessageBox("请先查询出报表内容！");
            }
        }
    }
}